﻿using AntdUI;
using IMES.Entity;
using IMES.Page;

namespace IMES
{
    /// <summary>
    /// 主页控制器
    /// </summary>
    public class MainController
    {
        private static MainController manager=new MainController ();
        private Dictionary<string, PageElement> pages=new Dictionary<string, PageElement> ();
        private PageElement? ActivePage = null;
        private int MaxTabItemNum = 10;
        // 窗体MAP
        private Dictionary<string, Type> ucPagesMap = new Dictionary<string, Type>();

        public MainController()
        {
            // 订单
            ucPagesMap.Add("mi_order_import", typeof(UC_OrderImport));
            ucPagesMap.Add("mi_order_manage",typeof(UC_OrderManage));
            // 裁床
            ucPagesMap.Add("mi_cut_step", typeof(UC_CutStep));
            ucPagesMap.Add("mi_cut_manage", typeof(UC_CutManage));
            ucPagesMap.Add("mi_cut_sendpe", typeof(UC_CutSendPe));
            ucPagesMap.Add("mi_cut_peback", typeof(UC_CutPeBack));
            ucPagesMap.Add("mi_cut_instore", typeof(UC_CutInstore));
            ucPagesMap.Add("mi_cut_outsend", typeof(UC_CutOutSend));
            // 车缝
            ucPagesMap.Add("mi_sew_plan", typeof(UC_SewPlan));
            ucPagesMap.Add("mi_sew_step", typeof(UC_SewStep));
            // 尾部
            ucPagesMap.Add("mi_pack_prelist", typeof(UC_PackPreList));
            ucPagesMap.Add("mi_pack_scan", typeof(UC_PackScan));
            ucPagesMap.Add("mi_pack_ship", typeof(UC_PackShip));
            // 成品仓
            ucPagesMap.Add("mi_warehouse_instore", typeof(UC_WHInstore));
            ucPagesMap.Add("mi_warehouse_out", typeof(UC_WHOut));
            // 基础资料
            ucPagesMap.Add("mi_basic_factory", typeof(UC_BasicFactory));
            ucPagesMap.Add("mi_basic_line", typeof(UC_BasicCustomer));
            ucPagesMap.Add("mi_basic_customer", typeof(UC_BasicCustomer));
            ucPagesMap.Add("mi_basic_size", typeof(UC_BasicSize));
            // 系统管理
            ucPagesMap.Add("mi_sys_roleuser", typeof(UC_SysRoleUser));
            ucPagesMap.Add("mi_sys_reportconfig", typeof(UC_SysReportConfig));
        }

        /// <summary>
        /// 实例
        /// </summary>
        public static MainController Instance
        { get { return manager; } }


        /// <summary>
        /// 左侧导航菜单选中
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="tab"></param>
        /// <param name="menuitem"></param>
        public void ActiveMenu(Form thisform,AntdUI.Panel panel, TabHeader tab, MenuItem menuitem)
        {
            if (string.IsNullOrWhiteSpace(menuitem.Name))
            {
                AntdUI.Message.error(thisform, "无法打开，页面错误!", thisform.Font);
                return;
            }

            if (ActivePage != null && ActivePage.Content != null)
            {
                ActivePage.Content.Visible = false;
                ActivePage.tabItem!.ShowClose = false;
            }

            if (!pages.ContainsKey(menuitem.Name))
            {
                //超过最大数量则关闭多余
                if (tab.Items.Count >= MaxTabItemNum)
                {
                    string pageName = tab.Items[0].Tag!.ToString()!;
                    tab.Items.RemoveAt(0);
                    var rmPage = pages[pageName];
                    rmPage.tabItem = null;
                    rmPage.Content!.Dispose();
                    pages.Remove(pageName);

                    AntdUI.Message.info(thisform, $"最多只能同时打开{MaxTabItemNum}个窗口,将关闭第一个!", thisform.Font);
                }

                var tempTabItem = new AntdUI.TagTabItem() { IconSvg = menuitem.IconSvg, Text = menuitem.Text!, Loading = true,Tag=menuitem.Name };
                tab.Items.Add(tempTabItem);
                var pageEl = new PageElement { pageName = menuitem.Name, tabItem = tempTabItem, Content = InitControl(panel,menuitem.Name) };
                ActivePage=pageEl;

                pages.Add(menuitem.Name, pageEl);
                //选中
                pageEl.tabItem.ShowClose = true;
                tab.Select(pageEl.tabItem);
                ActivePage.Content.Visible = true;

                tempTabItem.Loading = false;
            }
            else
            {
                ActivePage = pages[menuitem.Name];
                tab.Select(ActivePage.tabItem!);

                ActivePage.tabItem!.ShowClose = true;
                ActivePage.Content!.Visible = true;
            }
        }

        private Control InitControl(AntdUI.Panel parent,string pageName)
        {
            if (ucPagesMap.ContainsKey(pageName))
            {
                BasePage newPage = (BasePage)Activator.CreateInstance(ucPagesMap[pageName])!;
                newPage.Dock = DockStyle.Fill;
                if (newPage.InitData())
                {
                    parent.Controls.Add(newPage);
                }
                return newPage;
            }
            else
            {
                Control control = new AntdUI.Label() { BackColor = Color.Beige, Text = pageName + " 页面未配置!", Visible = false, Dock = DockStyle.Fill };
                parent.Controls.Add(control);
                return control;
            }
        }
 
        public void ClosePage(Form thisform, AntdUI.Panel panel,TabHeader tab,TagTabItem tabitem)
        {
            if(tabitem.Tag==null) return;
            string pageName= tabitem.Tag.ToString()!;
            if (!pages.ContainsKey(pageName)) return;

            var closePage=pages[pageName];
            pages.Remove(pageName);
            closePage.Content!.Dispose();
 
            if(tab.SelectedIndex==0 && tab.Items.Count>0)
            {
                tab.Select(tab.Items[0]);
            }
        }

        internal void PageActiveChanged(Form1 form1, AntdUI.Panel panel_Controls, TabHeader tab, TagTabItem tabItem)
        {
            if (tabItem != null && tabItem.Tag!=null && ActivePage != null && ActivePage.pageName != tabItem.Tag.ToString()!) {
                string pageName = tabItem.Tag.ToString()!;

                ActivePage.Content!.Visible = false;
                ActivePage.tabItem!.ShowClose = false;

                var newActivePage = pages[pageName];
                ActivePage = newActivePage;
                newActivePage.tabItem!.ShowClose = true;
                tab.Select(newActivePage.tabItem!);
                newActivePage.Content!.Visible = true;
            }
        }

        /// <summary>
        /// 当前登录的用户
        /// </summary>
        private User? _user = null;
        /// <summary>
        /// 当前登录用户
        /// </summary>
        public User? ThisLoginUser
        {
            get
            {
                return _user;
            }
        }
    }

    /// <summary>
    /// 页面内容
    /// </summary>
    public class PageElement
    {
        public string pageName {  get; set; }=string.Empty;
        public TagTabItem? tabItem { get; set; }
        public Control? Content { get; set; }
    }
}
